package com.st.bottomnavigation.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.st.bottomnavigation.R;


public class LoadingDialog extends Dialog {

    public LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;//上下文
        private String message;//提示信息
        private boolean isShowMessage = true;//是否显示提示信息
        private boolean isCancelable = false;//返回键是否可以取消
        private boolean isCancelOutside = false;//点击外部是否可以取消


        //构造方法传入上下文
        public Builder(Context context) {
            this.context = context;
        }

        //设置提示信息
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        //设置是否显示提示信息
        public Builder setShowMessage(boolean isShowMessage) {
            this.isShowMessage = isShowMessage;
            return this;
        }

        //设置是否可以按返回键取消
        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        //设置是否可以取消
        public Builder setCancelOutside(boolean isCancelOutside) {
            this.isCancelOutside = isCancelOutside;
            return this;
        }

        //创建LoadingDialog对象
        public LoadingDialog create() {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.dialog_loading, null);
            LoadingDialog loadingDailog = new LoadingDialog(context, R.style.MyProgressDialog);
            TextView msgText = (TextView) view.findViewById(R.id.messageTextView);
            if (isShowMessage) {
                msgText.setText(message);
            } else {
                msgText.setVisibility(View.GONE);
            }
            loadingDailog.setContentView(view);
            loadingDailog.setCancelable(isCancelable);
            loadingDailog.setCanceledOnTouchOutside(isCancelOutside);
            return loadingDailog;

        }
    }
}
